In [1]:
import tensorflow as tf
import tensorflow.contrib.timeseries as ts
from tensorflow.contrib.learn import learn_runner
from tensorflow.contrib.learn import make_export_strategy

import multiprocessing
import pandas as pd
import numpy as np
import shutil
from datetime import datetime

import matplotlib.pyplot as plt
#%matplotlib inline

print(tf.__version__)


/Users/khalidsalama/anaconda/lib/python3.6/importlib/_bootstrap.py:205: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
  return f(*args, **kwds)
1.4.0

In [2]:
MODEL_NAME = 'ts-model-02'

TRAIN_DATA_FILES = ['data/train-data.csv']
TEST_DATA_FILE = 'data/test-data.csv'

RESUME_TRAINING = False
MULTI_THREADING = True

Steps to use the ARRegressor + Experiment API

  1. Define the metadata
  2. Define a data (csv) input function
  3. Define a create Estimator function
  4. Run an Experiment with learn_runner to train, evaluate, and export the model
  5. Predict using the estimator
  6. Serve the saved model

1. Define Metadata


In [3]:
TIME_INDEX_FEATURE_NAME = 'time_index'
VALUE_FEATURE_NAMES = ['value']

2. Define a Data Input Function


In [4]:
def generate_input_fn(file_names, mode, skip_header_lines=1, batch_size = None, windows_size = None):
    
    columns = {
        ts.TrainEvalFeatures.TIMES: TIME_INDEX_FEATURE_NAME,
        ts.TrainEvalFeatures.VALUES: VALUE_FEATURE_NAMES
    }
    
    reader = tf.contrib.timeseries.CSVReader(filenames=file_names, 
                                    column_names=columns, 
                                    skip_header_lines=skip_header_lines)

    num_threads = multiprocessing.cpu_count() if MULTI_THREADING else 1
    
    if mode == tf.estimator.ModeKeys.TRAIN:
        input_fn = tf.contrib.timeseries.RandomWindowInputFn(
            reader, 
            batch_size=batch_size, 
            window_size=windows_size,
            num_threads= num_threads
        )
        
    elif mode == tf.estimator.ModeKeys.EVAL:
        input_fn = tf.contrib.timeseries.WholeDatasetInputFn(reader)
    
    return input_fn

3. Define a Create Estimator Function


In [5]:
def create_estimator(run_config, hparams):

    estimator = ts.ARRegressor(
        periodicities= hparams.periodicities, 
        input_window_size= hparams.input_window_size, 
        output_window_size= hparams.output_window_size,
        num_features=len(VALUE_FEATURE_NAMES),
        loss=hparams.loss,
        hidden_layer_sizes = hparams.hidden_units,
        
#         anomaly_prior_probability=hparams.anomaly_prob,
#         anomaly_distribution=hparams.anomaly_dist,
        
        optimizer = tf.train.AdagradOptimizer(learning_rate=hparams.learning_rate),
        config=run_config
    )
    
    print("")
    print("Estimator Type: {}".format(type(estimator)))
    print("")

    return estimator

4. Run Experiment

a. Set hyper-params values


In [6]:
CHECKPOINT_STEPS=1000

hparams  = tf.contrib.training.HParams(
    training_steps = 10000,
    periodicities = [200],
    input_window_size = 40,
    output_window_size=10,
    batch_size = 15,
    anomaly_prob = 0.5,
    anomaly_dist = "gaussian",
    loss = tf.contrib.timeseries.ARModel.NORMAL_LIKELIHOOD_LOSS, # NORMAL_LIKELIHOOD_LOSS | SQUARED_LOSS
    hidden_units = None,
    learning_rate = 0.1
    
)
  
model_dir = 'trained_models/{}'.format(MODEL_NAME)

run_config = tf.contrib.learn.RunConfig(
    save_checkpoints_steps=CHECKPOINT_STEPS,
    tf_random_seed=19831060,
    model_dir=model_dir
)

                                             
print("Model directory: {}".format(run_config.model_dir))
print("Hyper-parameters: {}".format(hparams))
print("")


Model directory: trained_models/ts-model-02
Hyper-parameters: [('anomaly_dist', 'gaussian'), ('anomaly_prob', 0.5), ('batch_size', 15), ('hidden_units', None), ('input_window_size', 40), ('learning_rate', 0.1), ('loss', 'normal_likelihood_loss'), ('output_window_size', 10), ('periodicities', [200]), ('training_steps', 10000)]

b. Define Experiment Function


In [7]:
def generate_experiment_fn(**experiment_args):
    
    train_input_fn = generate_input_fn(
        TRAIN_DATA_FILES,
        skip_header_lines=1,
        mode = tf.estimator.ModeKeys.TRAIN,
        batch_size=hparams.batch_size,
        windows_size = hparams.input_window_size + hparams.output_window_size
    )

    eval_input_fn = generate_input_fn(
        TRAIN_DATA_FILES,
        skip_header_lines=1,
        mode = tf.estimator.ModeKeys.EVAL,
        windows_size = hparams.input_window_size + hparams.output_window_size
    )

    def _experiment_fn(run_config, hparams):

        estimator = create_estimator(run_config, hparams)

        return tf.contrib.learn.Experiment(
            estimator,
            train_steps=hparams.training_steps,
            eval_steps=1,
            train_input_fn=train_input_fn,
            eval_input_fn=eval_input_fn,
            **experiment_args
        )

    return _experiment_fn

5. Run the Experiment


In [8]:
if not RESUME_TRAINING:
    print("Removing previous artifacts...")
    shutil.rmtree(model_dir, ignore_errors=True)
else:
    print("Resuming training...") 


tf.logging.set_verbosity(tf.logging.INFO)

time_start = datetime.utcnow() 
print("Experiment started at {}".format(time_start.strftime("%H:%M:%S")))
print(".......................................") 


learn_runner.run(
    experiment_fn=generate_experiment_fn(),
    run_config=run_config,
    schedule="train", #"train_and_evaluate"
    hparams=hparams
)

time_end = datetime.utcnow() 
print(".......................................")
print("Experiment finished at {}".format(time_end.strftime("%H:%M:%S")))
print("")
time_elapsed = time_end - time_start
print("Experiment elapsed time: {} seconds".format(time_elapsed.total_seconds()))


Removing previous artifacts...
Experiment started at 17:50:19
.......................................
WARNING:tensorflow:RunConfig.uid (from tensorflow.contrib.learn.python.learn.estimators.run_config) is experimental and may change or be removed at any time, and without warning.
INFO:tensorflow:Using config: {'_task_type': None, '_task_id': 0, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x11ef4e3c8>, '_master': '', '_num_ps_replicas': 0, '_num_worker_replicas': 0, '_environment': 'local', '_is_chief': True, '_evaluation_master': '', '_tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1
}
, '_tf_random_seed': 19831060, '_save_summary_steps': 100, '_save_checkpoints_secs': None, '_log_step_count_steps': 100, '_session_config': None, '_save_checkpoints_steps': 1000, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_model_dir': 'trained_models/ts-model-02'}

Estimator Type: <class 'tensorflow.contrib.timeseries.python.timeseries.estimators.ARRegressor'>

WARNING:tensorflow:RunConfig.uid (from tensorflow.contrib.learn.python.learn.estimators.run_config) is experimental and may change or be removed at any time, and without warning.
WARNING:tensorflow:From /Users/khalidsalama/anaconda/lib/python3.6/site-packages/tensorflow/contrib/timeseries/python/timeseries/head.py:63: get_global_step (from tensorflow.contrib.framework.python.ops.variables) is deprecated and will be removed in a future version.
Instructions for updating:
Please switch to tf.train.get_global_step
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into trained_models/ts-model-02/model.ckpt.
INFO:tensorflow:loss = 1.89026, step = 1
INFO:tensorflow:global_step/sec: 383.759
INFO:tensorflow:loss = 0.712069, step = 101 (0.265 sec)
INFO:tensorflow:global_step/sec: 412.752
INFO:tensorflow:loss = 0.670245, step = 201 (0.240 sec)
INFO:tensorflow:global_step/sec: 404.743
INFO:tensorflow:loss = 0.628767, step = 301 (0.248 sec)
INFO:tensorflow:global_step/sec: 410.268
INFO:tensorflow:loss = 0.473907, step = 401 (0.243 sec)
INFO:tensorflow:global_step/sec: 414.853
INFO:tensorflow:loss = 0.469355, step = 501 (0.244 sec)
INFO:tensorflow:global_step/sec: 368.666
INFO:tensorflow:loss = 0.598996, step = 601 (0.271 sec)
INFO:tensorflow:global_step/sec: 392.476
INFO:tensorflow:loss = 0.605418, step = 701 (0.252 sec)
INFO:tensorflow:global_step/sec: 434.607
INFO:tensorflow:loss = 0.469934, step = 801 (0.230 sec)
INFO:tensorflow:global_step/sec: 390.606
INFO:tensorflow:loss = 0.340511, step = 901 (0.260 sec)
INFO:tensorflow:Saving checkpoints for 1001 into trained_models/ts-model-02/model.ckpt.
INFO:tensorflow:global_step/sec: 111.496
INFO:tensorflow:loss = 0.428227, step = 1001 (0.893 sec)
INFO:tensorflow:global_step/sec: 324.321
INFO:tensorflow:loss = 0.37977, step = 1101 (0.311 sec)
INFO:tensorflow:global_step/sec: 411.574
INFO:tensorflow:loss = 0.386612, step = 1201 (0.241 sec)
INFO:tensorflow:global_step/sec: 407.697
INFO:tensorflow:loss = 0.462994, step = 1301 (0.244 sec)
INFO:tensorflow:global_step/sec: 392.954
INFO:tensorflow:loss = 0.355599, step = 1401 (0.255 sec)
INFO:tensorflow:global_step/sec: 365.234
INFO:tensorflow:loss = 0.26886, step = 1501 (0.274 sec)
INFO:tensorflow:global_step/sec: 368.666
INFO:tensorflow:loss = 0.2998, step = 1601 (0.274 sec)
INFO:tensorflow:global_step/sec: 365.803
INFO:tensorflow:loss = 0.323734, step = 1701 (0.274 sec)
INFO:tensorflow:global_step/sec: 394.734
INFO:tensorflow:loss = 0.370329, step = 1801 (0.250 sec)
INFO:tensorflow:global_step/sec: 435.72
INFO:tensorflow:loss = 0.239524, step = 1901 (0.230 sec)
INFO:tensorflow:Saving checkpoints for 2001 into trained_models/ts-model-02/model.ckpt.
INFO:tensorflow:global_step/sec: 113.502
INFO:tensorflow:loss = 0.337086, step = 2001 (0.884 sec)
INFO:tensorflow:global_step/sec: 333.96
INFO:tensorflow:loss = 0.20274, step = 2101 (0.296 sec)
INFO:tensorflow:global_step/sec: 428.525
INFO:tensorflow:loss = 0.176723, step = 2201 (0.236 sec)
INFO:tensorflow:global_step/sec: 397.106
INFO:tensorflow:loss = 0.29506, step = 2301 (0.252 sec)
INFO:tensorflow:global_step/sec: 374.749
INFO:tensorflow:loss = 0.30226, step = 2401 (0.267 sec)
INFO:tensorflow:global_step/sec: 387.418
INFO:tensorflow:loss = 0.20986, step = 2501 (0.258 sec)
INFO:tensorflow:global_step/sec: 372.147
INFO:tensorflow:loss = 0.224058, step = 2601 (0.270 sec)
INFO:tensorflow:global_step/sec: 404.565
INFO:tensorflow:loss = 0.40155, step = 2701 (0.246 sec)
INFO:tensorflow:global_step/sec: 372.898
INFO:tensorflow:loss = 0.240097, step = 2801 (0.267 sec)
INFO:tensorflow:global_step/sec: 389.28
INFO:tensorflow:loss = 0.323547, step = 2901 (0.259 sec)
INFO:tensorflow:Saving checkpoints for 3001 into trained_models/ts-model-02/model.ckpt.
INFO:tensorflow:global_step/sec: 111.656
INFO:tensorflow:loss = 0.357234, step = 3001 (0.893 sec)
INFO:tensorflow:global_step/sec: 419.542
INFO:tensorflow:loss = 0.336304, step = 3101 (0.239 sec)
INFO:tensorflow:global_step/sec: 358.335
INFO:tensorflow:loss = 0.275976, step = 3201 (0.281 sec)
INFO:tensorflow:global_step/sec: 400.245
INFO:tensorflow:loss = 0.196324, step = 3301 (0.250 sec)
INFO:tensorflow:global_step/sec: 400.126
INFO:tensorflow:loss = 0.182717, step = 3401 (0.250 sec)
INFO:tensorflow:global_step/sec: 408.489
INFO:tensorflow:loss = 0.156522, step = 3501 (0.243 sec)
INFO:tensorflow:global_step/sec: 383.684
INFO:tensorflow:loss = 0.139308, step = 3601 (0.260 sec)
INFO:tensorflow:global_step/sec: 363.677
INFO:tensorflow:loss = 0.190825, step = 3701 (0.274 sec)
INFO:tensorflow:global_step/sec: 414.23
INFO:tensorflow:loss = 0.286995, step = 3801 (0.243 sec)
INFO:tensorflow:global_step/sec: 397.988
INFO:tensorflow:loss = 0.331847, step = 3901 (0.250 sec)
INFO:tensorflow:Saving checkpoints for 4001 into trained_models/ts-model-02/model.ckpt.
INFO:tensorflow:global_step/sec: 107.272
INFO:tensorflow:loss = 0.137182, step = 4001 (0.931 sec)
INFO:tensorflow:global_step/sec: 360.352
INFO:tensorflow:loss = 0.205149, step = 4101 (0.281 sec)
INFO:tensorflow:global_step/sec: 363.617
INFO:tensorflow:loss = 0.0612294, step = 4201 (0.278 sec)
INFO:tensorflow:global_step/sec: 388.084
INFO:tensorflow:loss = 0.172613, step = 4301 (0.253 sec)
INFO:tensorflow:global_step/sec: 381.717
INFO:tensorflow:loss = 0.292768, step = 4401 (0.263 sec)
INFO:tensorflow:global_step/sec: 446.64
INFO:tensorflow:loss = 0.341345, step = 4501 (0.223 sec)
INFO:tensorflow:global_step/sec: 393.38
INFO:tensorflow:loss = 0.15775, step = 4601 (0.255 sec)
INFO:tensorflow:global_step/sec: 368.688
INFO:tensorflow:loss = 0.0528084, step = 4701 (0.270 sec)
INFO:tensorflow:global_step/sec: 418.911
INFO:tensorflow:loss = 0.286441, step = 4801 (0.239 sec)
INFO:tensorflow:global_step/sec: 388.582
INFO:tensorflow:loss = 0.212251, step = 4901 (0.261 sec)
INFO:tensorflow:Saving checkpoints for 5001 into trained_models/ts-model-02/model.ckpt.
INFO:tensorflow:global_step/sec: 109.681
INFO:tensorflow:loss = 0.174229, step = 5001 (0.908 sec)
INFO:tensorflow:global_step/sec: 369.419
INFO:tensorflow:loss = 0.260405, step = 5101 (0.272 sec)
INFO:tensorflow:global_step/sec: 366.924
INFO:tensorflow:loss = 0.161832, step = 5201 (0.272 sec)
INFO:tensorflow:global_step/sec: 352.576
INFO:tensorflow:loss = 0.0545976, step = 5301 (0.281 sec)
INFO:tensorflow:global_step/sec: 401.566
INFO:tensorflow:loss = 0.25474, step = 5401 (0.253 sec)
INFO:tensorflow:global_step/sec: 361.771
INFO:tensorflow:loss = 0.114917, step = 5501 (0.276 sec)
INFO:tensorflow:global_step/sec: 411.567
INFO:tensorflow:loss = 0.305715, step = 5601 (0.241 sec)
INFO:tensorflow:global_step/sec: 462.869
INFO:tensorflow:loss = 0.12577, step = 5701 (0.217 sec)
INFO:tensorflow:global_step/sec: 396.068
INFO:tensorflow:loss = 0.185293, step = 5801 (0.255 sec)
INFO:tensorflow:global_step/sec: 392.073
INFO:tensorflow:loss = 0.217243, step = 5901 (0.252 sec)
INFO:tensorflow:Saving checkpoints for 6001 into trained_models/ts-model-02/model.ckpt.
INFO:tensorflow:global_step/sec: 109.092
INFO:tensorflow:loss = 0.170612, step = 6001 (0.918 sec)
INFO:tensorflow:global_step/sec: 355.009
INFO:tensorflow:loss = 0.120756, step = 6101 (0.281 sec)
INFO:tensorflow:global_step/sec: 372.23
INFO:tensorflow:loss = 0.166662, step = 6201 (0.267 sec)
INFO:tensorflow:global_step/sec: 391.742
INFO:tensorflow:loss = 0.216219, step = 6301 (0.258 sec)
INFO:tensorflow:global_step/sec: 392.963
INFO:tensorflow:loss = 0.271654, step = 6401 (0.254 sec)
INFO:tensorflow:global_step/sec: 407.161
INFO:tensorflow:loss = 0.234456, step = 6501 (0.245 sec)
INFO:tensorflow:global_step/sec: 419.361
INFO:tensorflow:loss = 0.149325, step = 6601 (0.236 sec)
INFO:tensorflow:global_step/sec: 404.714
INFO:tensorflow:loss = 0.187818, step = 6701 (0.250 sec)
INFO:tensorflow:global_step/sec: 402.381
INFO:tensorflow:loss = 0.181293, step = 6801 (0.247 sec)
INFO:tensorflow:global_step/sec: 397.682
INFO:tensorflow:loss = -0.00449062, step = 6901 (0.253 sec)
INFO:tensorflow:Saving checkpoints for 7001 into trained_models/ts-model-02/model.ckpt.
INFO:tensorflow:global_step/sec: 132.732
INFO:tensorflow:loss = 0.130109, step = 7001 (0.753 sec)
INFO:tensorflow:global_step/sec: 441.989
INFO:tensorflow:loss = 0.230179, step = 7101 (0.225 sec)
INFO:tensorflow:global_step/sec: 447.444
INFO:tensorflow:loss = 0.204787, step = 7201 (0.223 sec)
INFO:tensorflow:global_step/sec: 387.694
INFO:tensorflow:loss = 0.105013, step = 7301 (0.261 sec)
INFO:tensorflow:global_step/sec: 376.921
INFO:tensorflow:loss = 0.103757, step = 7401 (0.268 sec)
INFO:tensorflow:global_step/sec: 453.976
INFO:tensorflow:loss = 0.0711738, step = 7501 (0.216 sec)
INFO:tensorflow:global_step/sec: 397.059
INFO:tensorflow:loss = 0.0715671, step = 7601 (0.254 sec)
INFO:tensorflow:global_step/sec: 419.146
INFO:tensorflow:loss = 0.161912, step = 7701 (0.239 sec)
INFO:tensorflow:global_step/sec: 435.167
INFO:tensorflow:loss = 0.157793, step = 7801 (0.230 sec)
INFO:tensorflow:global_step/sec: 390.547
INFO:tensorflow:loss = 0.117504, step = 7901 (0.255 sec)
INFO:tensorflow:Saving checkpoints for 8001 into trained_models/ts-model-02/model.ckpt.
INFO:tensorflow:global_step/sec: 114.955
INFO:tensorflow:loss = 0.274265, step = 8001 (0.869 sec)
INFO:tensorflow:global_step/sec: 386.547
INFO:tensorflow:loss = 0.147918, step = 8101 (0.262 sec)
INFO:tensorflow:global_step/sec: 391.664
INFO:tensorflow:loss = 0.204396, step = 8201 (0.252 sec)
INFO:tensorflow:global_step/sec: 430.163
INFO:tensorflow:loss = 0.163874, step = 8301 (0.235 sec)
INFO:tensorflow:global_step/sec: 391.527
INFO:tensorflow:loss = 0.0800651, step = 8401 (0.253 sec)
INFO:tensorflow:global_step/sec: 398.687
INFO:tensorflow:loss = 0.0720757, step = 8501 (0.252 sec)
INFO:tensorflow:global_step/sec: 487.912
INFO:tensorflow:loss = 0.0996263, step = 8601 (0.204 sec)
INFO:tensorflow:global_step/sec: 430.655
INFO:tensorflow:loss = 0.0827273, step = 8701 (0.233 sec)
INFO:tensorflow:global_step/sec: 420.544
INFO:tensorflow:loss = 0.184251, step = 8801 (0.236 sec)
INFO:tensorflow:global_step/sec: 405.622
INFO:tensorflow:loss = 0.0958425, step = 8901 (0.250 sec)
INFO:tensorflow:Saving checkpoints for 9001 into trained_models/ts-model-02/model.ckpt.
INFO:tensorflow:global_step/sec: 101.295
INFO:tensorflow:loss = 0.134386, step = 9001 (0.987 sec)
INFO:tensorflow:global_step/sec: 364.453
INFO:tensorflow:loss = 0.129565, step = 9101 (0.271 sec)
INFO:tensorflow:global_step/sec: 364.966
INFO:tensorflow:loss = 0.13404, step = 9201 (0.274 sec)
INFO:tensorflow:global_step/sec: 356.204
INFO:tensorflow:loss = 0.0628099, step = 9301 (0.286 sec)
INFO:tensorflow:global_step/sec: 377.055
INFO:tensorflow:loss = 0.0512348, step = 9401 (0.261 sec)
INFO:tensorflow:global_step/sec: 397.245
INFO:tensorflow:loss = 0.177017, step = 9501 (0.252 sec)
INFO:tensorflow:global_step/sec: 388.041
INFO:tensorflow:loss = 0.258591, step = 9601 (0.257 sec)
INFO:tensorflow:global_step/sec: 389.052
INFO:tensorflow:loss = 0.154202, step = 9701 (0.259 sec)
INFO:tensorflow:global_step/sec: 401.204
INFO:tensorflow:loss = 0.00609266, step = 9801 (0.249 sec)
INFO:tensorflow:global_step/sec: 370.217
INFO:tensorflow:loss = 0.0638521, step = 9901 (0.269 sec)
INFO:tensorflow:Saving checkpoints for 10000 into trained_models/ts-model-02/model.ckpt.
INFO:tensorflow:Loss for final step: 0.0844589.
.......................................
Experiment finished at 17:50:55

Experiment elapsed time: 36.410878 seconds

6. Evalute the Estimator


In [9]:
hparams.loss = tf.contrib.timeseries.ARModel.SQUARED_LOSS # NORMAL_LIKELIHOOD_LOSS | SQUARED_LOSS
 
estimator = create_estimator(run_config, hparams)

eval_input_fn = generate_input_fn(
    file_names=TRAIN_DATA_FILES,
    mode = tf.estimator.ModeKeys.EVAL,
)

tf.logging.set_verbosity(tf.logging.WARN)
evaluation = estimator.evaluate(input_fn=eval_input_fn, steps=1)
print("")
print(evaluation.keys())
print("")
print("Evaluation Loss ({}) : {}".format(hparams.loss, evaluation['loss']))


INFO:tensorflow:Using config: {'_task_type': None, '_task_id': 0, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x11ef4e3c8>, '_master': '', '_num_ps_replicas': 0, '_num_worker_replicas': 0, '_environment': 'local', '_is_chief': True, '_evaluation_master': '', '_tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1
}
, '_tf_random_seed': 19831060, '_save_summary_steps': 100, '_save_checkpoints_secs': None, '_log_step_count_steps': 100, '_session_config': None, '_save_checkpoints_steps': 1000, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_model_dir': 'trained_models/ts-model-02'}

Estimator Type: <class 'tensorflow.contrib.timeseries.python.timeseries.estimators.ARRegressor'>

WARNING:tensorflow:Skipping summary for covariance, must be a float, np.float32, np.int64, np.int32 or int.
WARNING:tensorflow:Skipping summary for mean, must be a float, np.float32, np.int64, np.int32 or int.
WARNING:tensorflow:Skipping summary for observed, must be a float, np.float32, np.int64, np.int32 or int.
WARNING:tensorflow:Skipping summary for start_tuple, must be a float, np.float32, np.int64, np.int32 or int.
WARNING:tensorflow:Skipping summary for times, must be a float, np.float32, np.int64, np.int32 or int.

dict_keys(['covariance', 'loss', 'mean', 'observed', 'start_tuple', 'times', 'global_step'])

Evaluation Loss (squared_loss) : 0.19278353452682495

In [10]:
def compute_rmse(a, b):
    rmse =  np.sqrt(np.sum(np.square(a - b)) / len(a))
    return rmse

def compute_mae(a, b):
    rmse =  np.sqrt(np.sum(np.abs(a - b)) / len(a))
    return rmse

In [11]:
x_current = evaluation['times'][0]
y_current_actual = evaluation['observed'][0].reshape(-1)
y_current_estimated = evaluation['mean'][0].reshape(-1)

rmse = compute_rmse(y_current_actual, y_current_estimated)
mae = compute_mae(y_current_actual, y_current_estimated)
print("Evaluation RMSE {}".format(rmse))
print("Evaluation MAE {}".format(mae))


Evaluation RMSE 0.4390712382149373
Evaluation MAE 0.5812069138274865

In [12]:
plt.figure(figsize=(20, 10))

plt.title("Time Series Data")
plt.plot(x_current, y_current_actual, label='actual')
plt.plot(x_current, y_current_estimated, label='estimated')
plt.xlabel("Time Index")
plt.ylabel("Value")
plt.legend(loc=2)
plt.show()


7. Predict using the Estimator


In [22]:
FORECAST_STEPS = [10,50,100,150,200,250,300]

tf.logging.set_verbosity(tf.logging.ERROR)

eval_input_fn = generate_input_fn(
    file_names =TRAIN_DATA_FILES,
    mode = tf.estimator.ModeKeys.EVAL
)


evaluation = estimator.evaluate(input_fn=eval_input_fn, steps=1)

df_test = pd.read_csv(TEST_DATA_FILE, names=['time_index','value'], header=0)
print("Test Dataset Size: {}".format(len(df_test)))
print("")

for steps in FORECAST_STEPS:

    forecasts = estimator.predict(input_fn=ts.predict_continuation_input_fn(evaluation, steps=steps))
    forecasts = tuple(forecasts)[0]
    
    x_next = forecasts['times']
    
    y_next_forecast = forecasts['mean']
    y_next_actual = df_test.value[:steps].values
    
    rmse =  compute_rmse(y_next_actual, y_next_forecast)
    mae =  compute_mae(y_next_actual, y_next_forecast)

    print("Forecast Steps {}: RMSE {} - MAE {}".format(steps,rmse,mae))

print("")
print(forecasts.keys())


Test Dataset Size: 300

Forecast Steps 10: RMSE 1.9936394778372102 - MAE 2.2743827214709342
Forecast Steps 50: RMSE 4.591944294681093 - MAE 5.160628494779687
Forecast Steps 100: RMSE 7.566335809273664 - MAE 7.772297081132177
Forecast Steps 150: RMSE 16.471776556868893 - MAE 12.724165150969178
Forecast Steps 200: RMSE 20.98392475881654 - MAE 15.546772532587175
Forecast Steps 250: RMSE 21.433018212770218 - MAE 16.607481520114312
Forecast Steps 300: RMSE 22.322000108274846 - MAE 17.71527927263785

dict_keys(['mean', 'covariance', 'times'])

In [14]:
plt.close('all')
plt.figure(figsize=(20, 10))

plt.title("Time Series Data")
plt.plot(x_next, y_next_actual, label='actual')
plt.plot(x_next, y_next_forecast, label='forecasted')
plt.xlabel("Time Index")
plt.ylabel("Value")
plt.legend(loc=2)
plt.show()



In [15]:
x_all = np.concatenate( (x_current, x_next) , axis=0)
y_actual_all = np.concatenate((y_current_actual, y_next_actual), axis=0)

plt.close('all')
plt.figure(figsize=(20, 10))

plt.title("Time Series Data")
plt.plot(x_all, y_actual_all, label='actual')
plt.plot(x_current, y_current_estimated, label='estimated')
plt.plot(x_next, y_next_forecast, label='forecasted')
plt.xlabel("Time Index")
plt.ylabel("Value")
plt.legend(loc=2)
plt.show()


8. Save & Serve Model


In [19]:
export_dir = model_dir + "/expo"

estimator.export_savedmodel(
    export_dir_base=export_dir,
    serving_input_receiver_fn=estimator.build_raw_serving_input_receiver_fn(),
    as_text=True
)


Out[19]:
b'trained_models/ts-model-02/expo/1510691426'

In [66]:
import os

saved_model_dir = export_dir +"/"+os.listdir(path=export_dir)[-1] 

input_values = df_test.value[:40].values

print(saved_model_dir)

predictor_fn = tf.contrib.predictor.from_saved_model(
    export_dir = saved_model_dir
)

times = np.arange(1,250)

output = predictor_fn(
    {
        "model_state_00":[input_values],
        "model_state_01":input_values.reshape(1,40,1),
        "times": [times]
    }

)
predictions = list(map(lambda ls: ls[0],output["mean"][0]))


trained_models/ts-model-02/expo/1510691426

In [67]:
plt.close('all')
plt.figure(figsize=(20, 10))

plt.title("Time Series Data")
plt.plot(times, predictions, label='actual')
plt.xlabel("Time Index")
plt.ylabel("Value")
plt.legend(loc=2)
plt.show()



In [ ]: